package com.ld.security.demo.event;

import com.hankcs.hanlp.HanLP;
import com.ld.security.auth.common.sys.entity.SysUserEntity;
import com.ld.security.auth.common.sys.service.ISysUserService;
import com.ld.security.auth.shiro.util.LoginUtil;
import com.ld.security.common.core.base.ESuperEntity;
import com.ld.security.common.core.util.StringUtil;
import com.ld.security.common.core.util.UuidUtil;
import com.ld.security.common.mybatis.base.entity.EFromUserEntity;
import com.ld.security.common.mybatis.base.entity.EToUserEntity;
import com.ld.security.common.mybatis.base.entity.SuperEntity;
import com.ld.security.common.mybatis.base.vo.ProxyVo;
import com.ld.security.common.mybatis.core.entity.SqlUserEntity;
import com.ld.security.common.mybatis.event.SaveEvent;
import com.ld.security.common.mybatis.util.JdbcUtil;
import com.ld.security.server.admin.modules.ge.sql.SqlProjectPo;
import com.ld.security.server.admin.modules.ge.util.ProjectUtil;
import com.ld.security.server.blog.modules.cms.entity.CmsCollectEntity;
import com.ld.security.server.blog.modules.cms.entity.CmsContentEntity;
import com.ld.security.server.user.modules.rec.util.RecUtil;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SimpSaveEvent implements SaveEvent {



    @Autowired
    ISysUserService iSysUserService;

    @Override
    public void saveBefore(ESuperEntity obj) {

        if(obj instanceof CmsContentEntity){
            CmsContentEntity col = (CmsContentEntity)obj;
            String di =  getHtmlDigest(col.getContent());
            col.setDigest(di);
        }

        if(obj instanceof CmsCollectEntity){
            CmsCollectEntity col = (CmsCollectEntity)obj;
            String di =  getHtmlDigest(col.getContent());
            col.setDigest(di);
        }


        if(obj instanceof CmsCollectEntity){
            CmsCollectEntity col = (CmsCollectEntity)obj;
            String di =  getHtmlDigest(col.getContent());
            col.setDigest(di);
        }



        if(obj instanceof SqlUserEntity){
            SqlUserEntity crtUserEntity = (SqlUserEntity) obj;
            if(StringUtil.isNull(crtUserEntity.getCrtUser())){
                crtUserEntity.setCrtUser(LoginUtil.getUserId());
            }
        }

        if(obj instanceof SqlProjectPo){
            SqlProjectPo po =(SqlProjectPo) obj;
            if(StringUtil.isNull( po.getProjectId())){
                String projectId =  ProjectUtil.getDefProectId();
                po.setProjectId(projectId);
            }

        }

        if(obj instanceof EFromUserEntity){
            EFromUserEntity msgEntity = (EFromUserEntity) obj;

            //空则拿自己作为为来源
            if(StringUtil.isNull(msgEntity.getFromUser())){
                msgEntity.setFromUser(LoginUtil.getUserId());
            }

            SysUserEntity fromUser  =  iSysUserService.getById(msgEntity.getFromUser());
            msgEntity.setFromUserName(fromUser.getLabel());
            msgEntity.setFromUserLogo(fromUser.getLogo());
        }



        if(obj instanceof EToUserEntity){
            EToUserEntity msgUserEntity = (EToUserEntity)obj;
            String toUserId =  msgUserEntity.getToUser();
            if (StringUtil.isNotNull(toUserId)) {
                SysUserEntity toUser  =  iSysUserService.getById(toUserId);
                msgUserEntity.setToUserName(toUser.getLabel());
                msgUserEntity.setToUserLogo(toUser.getLogo());
            }

        }



       // if(obj instanceof LabelMap){
            //LabelMap labelMap = (LabelMap)obj;
            JdbcUtil.proxyLabel(obj);
       // }

        if(obj instanceof ProxyVo){
            ProxyVo proxyVo = (ProxyVo)obj;
            proxyVo.saveBefore();
        }

        if(obj instanceof SuperEntity){

            UuidUtil.initIdKey(obj);

        }


    }

    @Override
    public void saveAfter(ESuperEntity val) {





        if(RecUtil.hasItemConver(val.getClass())){
            RecUtil.asyncSaveItem(val);
        }

    }



    private static int maxSize = 256;

    private static int maxDigest=10;

    public  static String getHtmlDigest(String content){
        content =  Jsoup.parse(content).text();
        return getDigest(content);
    }

    public  static String getDigest(String content){
        List<String> sentenceList = HanLP.extractSummary(content, maxDigest);

        StringBuffer sb = new StringBuffer();
        for (String s : sentenceList) {
            sb.append(s+",");
        }


        if(sb.length() > maxSize){
            sb.delete(maxSize,sb.length());

        }
        return sb.toString();
    }
}
